package Computer;

import java.util.*;
import Process.*;

/**
 * Keyboard class.
 * 
 * @author Stephen Amos, Zach Turk, Dickson Kwong
 *
 */

/**
 * This class manages a keyboard object.
 * 
 * @author Stephen Amos, Zach Turk, Dickson Kwong
 */
public class Keyboard extends Observable implements Observer 
{
	/**
	 * Specifies the size of the keyboard buffer.
	 */
	private static int BUFFER_SIZE = 100;
	
	/**
	 * The keyboard buffer.
	 */
	private Queue<Character> key_buffer;
	private static Queue<PCB> blocked;

	public Keyboard() 
	{
		key_buffer = new ArrayDeque<Character>(BUFFER_SIZE);
		blocked = new ArrayDeque<PCB>();
	}
	
	/**
	 * Adds the specified character to the keyboard buffer.
	 * 
	 * @param the_char The character to add.
	 */
	public void addToBuffer(char the_char)
	{
		key_buffer.add(the_char);
	}
	
	/**
	 * Returns the character at the front of the keyboard buffer queue
	 * and removes it from the keyboard buffer.
	 * 
	 * @return The character at the front of the keyboard buffer queue.
	 */
	public char readFromBuffer()
	{
		return key_buffer.poll();
	}
	
	/**
	 * Returns but does not delete the character at the front of the 
	 * keyboard buffer queue.
	 * 
	 * @return The character at the front of the keyboard buffer queue.
	 */
	public char peekBuffer()
	{
		return key_buffer.peek();
	}
	
	/**
	 * Determines if the keyboard buffer is empty or not.
	 * 
	 * @return True if and only if the keyboard buffer is empty.
	 */
	public boolean isBufferEmpty()
	{
		return key_buffer.isEmpty();
	}

	@Override
	public void update(Observable the_user_input, Object arg1) 
	{
		UserInput user_input = (UserInput) the_user_input;
		addToBuffer(user_input.getChar());

		setChanged();
		notifyObservers();
	}
	
	/**
	 * Add a process to this device's blocking queue.
	 * @param pcb
	 */
	public void addBlocked(PCB pcb) {
		blocked.add(pcb);
	}
	
	public PCB removeBlocked() {
		return blocked.poll();
	}
}

//char[] keyboard_buffer = new char[BUFFER_SIZE];
//int buffer_index = -1;
//
//public void pushToBuffer(char the_char)
//{
//		buffer_index++;
//		keyboard_buffer[buffer_index] = the_char;
//}
//
//public char popFromBuffer()
//{
//	char return_char = 0;
//	
//	if (buffer_index == -1)
//	{
//		//Buffer is empty
//		//What should be returned?
//	}
//	else
//	{
//		return_char = keyboard_buffer[buffer_index];
//		buffer_index--;
//	}
//	
//	return return_char;
//}
//
//public char peekBuffer()
//{
//	char return_char = 0;
//	
//	if (buffer_index == -1)
//	{
//		//Buffer is empty
//		//What should be returned?
//	}
//	else
//	{
//		return_char = keyboard_buffer[buffer_index];
//	}
//	
//	return keyboard_buffer[buffer_index];
//}
//
//public boolean isBufferEmpty()
//{
//	boolean is_empty;
//	
//	if (buffer_index == -1)
//	{
//		is_empty = true;
//	}
//	else
//	{
//		is_empty = false;
//	}
//	
//	return is_empty;
//}